package spiderUtil;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MovieSpider {
    private static final Logger logger = LoggerFactory.getLogger(MovieSpider.class); //创建日志对象

    //获取250电影详情页url 每页25个，修改参数start遍历10页
    private List<String> getDetailsURL() throws InterruptedException {
        List<String> detailsURL = new ArrayList<String>(250);

        //遍历10页
        for (int i = 0; i < 10; i++) {
            int offset = 25 * i;
            Connection connect = Jsoup.connect("https://movie.douban.com/top250")
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("start", "" + offset)
                    .data("filter", "");
            //带参数结束

            Document document = null;
            try {
                document = connect.get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (document == null)
                break;
            //class="pic" 的href属性

            //    <em class="">1</em>
            //       <a href="https://movie.douban.com/subject/1292052/">
            //           <img width="100" alt="肖申克的救赎" src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp" class="">
            //      </a>

            List<String> elements = document.select(".pic > a").eachAttr("href");
            //添加这页的25个url到list
            detailsURL.addAll(elements);

            Thread.sleep((long) (100 * Math.random()));
        }

        return detailsURL;
    }

    //根据url获取每个电影详情
    private Movie getDetails(String URL) {

        Movie movie = new Movie();
        Connection connect = Jsoup.connect(URL)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        //整个html
        Document document;
        try {
            document = connect.get();
        } catch (IOException e) {
            e.printStackTrace();
            return movie;
        }
        if (document == null)
            return movie;

        //----------直接从根节点 依据特定属性查找
        //名称  <span property="v:itemreviewed">肖申克的救赎 The Shawshank Redemption</span>
        movie.name = document.select("span[property=\"v:itemreviewed\"]").text();
        //评分 <strong class="ll rating_num" property="v:average">9.7</strong>
        movie.score = document.select("strong[ property=\"v:average\"]").text();
        //评分人数 <span property="v:votes">2076149</span>人评价
        movie.numOfPeopleScored = document.select("span[property=\"v:votes\"]").text();

        //<div id="info">中存放除评分以外信息
        Element allInfoElement = document.getElementById("info");

        //--根据span标签特定属性填充select条件
        //--(在网页中使用Ctrl+f 全局查找确认和要查找的数据一一对应)
        //--在<div id="info">中查找可以缩小查找范围，提高速度
        //导演 <a href="/celebrity/1010884/" rel="v:directedBy">埃里克·托莱达诺</a></span></span>
        movie.director = allInfoElement.select("a[rel=\"v:directedBy\"]").eachText();
        //主演 <a href="/celebrity/1054521/" rel="v:starring">蒂姆·罗宾斯</a>
        movie.actor = allInfoElement.select("a[rel=\"v:starring\"]").eachText();
        //类型 <span class="pl">类型:</span> <span property="v:genre">剧情</span> / <span property="v:genre">犯罪</span><br/>
        movie.type = allInfoElement.select("span[property=\"v:genre\"]").eachText();
        //发行日期 <span property="v:initialReleaseDate" content="1994-09-10(多伦多电影节)">1994-09-10(多伦多电影节)</span>
        movie.releaseDate = allInfoElement.select("span[property=\"v:initialReleaseDate\"]").eachText();
        //片长 <span class="pl">片长:</span> <span property="v:runtime" content="142">142分钟</span><br/>
        movie.filmLength = allInfoElement.select("span[property=\"v:runtime\"]").text();

        //--在<span class=pl>后的文本内容（Node而不是Element），使用nextslibing而不是nextElementSlibing
        Elements plSpan = allInfoElement.select("span.pl ");
        // 国家/地区 <span class="pl">制片国家/地区:</span> 美国<br/>
        // 语言 <span class="pl">语言:</span> 英语<br/> 用nextSlibing而不是nextElementSlibing获取文本(Node)
        // 别名 <span class="pl">又名:</span> 月黑高飞(港) / 刺激1995(台) / 地狱诺言 / 铁窗岁月 / 消香克的救赎<br/>
        for (Element tmp : plSpan) {
            switch (tmp.text()) {
                case "制片国家/地区:":
                    movie.region = tmp.nextSibling().toString();
                    break;
                case "语言:":
                    movie.language = tmp.nextSibling().toString();
                    break;
                case "又名:":
                    movie.alias = tmp.nextSibling().toString();
                    break;
            }
        }

        //编剧 <span id = "info">的第1个 直接子</span>元素 的 所有子</a>元素
        movie.screenwriter = allInfoElement.select("> span").get(1).getElementsByTag("a").eachText();

        return movie;
    }

    public void wirteToLog() throws InterruptedException {
        List<String> detailsURL = getDetailsURL();

        for (String url :
                detailsURL) {
            Movie movie = getDetails(url);

            String line = movie.name + "\t" +
                    movie.score + "\t" +
                    movie.numOfPeopleScored + "\t" +
                    movie.director + "\t" +
                    movie.screenwriter + "\t" +
                    movie.actor + "\t" +
                    movie.type + "\t" +
                    movie.region + "\t" +
                    movie.language + "\t" +
                    movie.releaseDate + "\t" +
                    movie.filmLength + "\t" +
                    movie.alias;
            //写入一行结果
            logger.info(line);

            Thread.sleep((long) (100 * Math.random()));//避免被封
        }
    }
}
